<?php
/**
 * Filter component
 * @static
 * @author Kirill A Egorov 2010
 */
class Filter
{
	static protected $_urlDelimetr = '/';
	/**
	 * Set url params delimetr
	 * @param string $urlDelimetr
	 */
	static public function setDelimetr($urlDelimetr){
		self::$_urlDelimetr = $urlDelimetr;
	}
	/**
	 * String cleanup
	 * @param string $string
	 * @return string
	 */
    static public function filterString($string){
        return trim(strip_tags($string));
    }
    /**
     * Filter variable
     * @param string $filter
     * @param mixed $value
     * @return mixed
     */
    static public function filterValue($filter , $value)
    {
        $filter = strtolower($filter);
        switch($filter)
        {
            case 'array' : if(!is_array($value))
                              $value  = array($value);
                      break;            
            case 'bool':
            case 'boolean':  $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);    
                      break;    
             case 'int' :
             case 'integer' : $value = intval($value);
                              break;
             case 'float' :
             case 'decimal':
             case 'number' : $value = floatval($value);  
                             break;
             case 'str'	:
             case 'string':
             case 'text':    $value = self::filterString($value);
                             break; 

             case 'raw':     break;
            
             case 'alphanum':
             case 'login' : $value  =  preg_replace("/[^A-Za-z0-9_]/i",'',$value);      
                             break;   
             case 'pagecode': 
             				$value = strtolower($value);
             				switch (self::$_urlDelimetr)
             				{
             					case '_' :
             						$value = str_replace(array(' ','_'),"-" ,$value);
             						break;
             					case '-':
             						$value = str_replace(array(' ','-'),"_" ,$value);
             						break;	
             				}          					
             				$value = preg_replace("/[^a-z0-9_-]/i",'',str_replace(array('-',' ','_'),"" ,strtolower($value)));     
                            break;                            
  
             default :      $value = intval($value);   
                            break;        
     
        }
        return $value;
    }
}